package cn.zlp.user.controller;


import cn.zlp.basic.util.AjaxResult;
import cn.zlp.basic.util.PageList;
import cn.zlp.user.domain.Logininfo;
import cn.zlp.user.query.LogininfoQuery;
import cn.zlp.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登录信息控制接口
 * @author Mr.Zhang
 */
@RestController
@RequestMapping("/logininfo")
@Api(tags = "登录信息接口",description = "登录信息接口详细描述")
public class LogininfoController {

    @Autowired
    private ILogininfoService logininfoService;

    /** 查询所有
     * 定义一个方法：返回值：List<Logininfo> +  方法名：findAll + 参数列表：无
     * 处理请求：Get http://localhost:8080/logininfo
     * GetMapping等价于RequestMapping(value = "",method = RequestMethod.GET)
     */
    @GetMapping
    @ApiOperation(value = "查询所有接口",notes = "")
    public List<Logininfo> findAll(){
        return logininfoService.findAll();
    }

    /** 查询单个
     * 以前处理请求：Get http://localhost:8080/logininfo?id=1
     * 现在处理请求：Get http://localhost:8080/logininfo/1
     * GetMapping("/{id}")用{}区分是请求参数，没有用{}是请求地址
     * PathVariable用来接收传过来的参数id
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个接口",notes = "需要传入参数id")
    public Logininfo findOne(@PathVariable("id")Long id){
        return logininfoService.findById(id);
    }

    /** 分页查询 */
    @PostMapping
    @ApiOperation(value = "分页查询接口",notes = "需要传入分页参数")
    public PageList<Logininfo> pageList(@RequestBody LogininfoQuery logininfoQuery){
        PageList<Logininfo> list = logininfoService.pageQuery(logininfoQuery);
        return list;
    }


    /** 删除 */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口",notes = "需要传入参数id")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            logininfoService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("删除失败");
        }
    }


    /** 批量删除 */
    @PatchMapping
    @ApiOperation(value = "批量删除接口",notes = "需要传入参数id集合")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            logininfoService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /** 修改 */
    @PutMapping
    @ApiOperation(value = "修改接口",notes = "需要传入登录信息参数")
    public AjaxResult saveOrUpdate(@RequestBody Logininfo logininfo){
        try {
            // 调用update方法
            logininfoService.update(logininfo);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("操作失败");
        }
    }

}
